﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DuJianliang.ApplicationPlatform.Flow
{
    public partial class FlowStepInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strFlowInstanceID = Request.QueryString["FlowInstanceID"];
                Guid guidFlowInstanceID = string.IsNullOrWhiteSpace(strFlowInstanceID) ? Guid.Empty : new Guid(strFlowInstanceID);
                BindGridView(guidFlowInstanceID);
            }  
        }

        private void BindGridView(Guid flowInstanceID)
        {
            string getToDoSQL = @"SELECT * FROM dbo.FlowInstanceStep WHERE FlowInstanceID = @FlowInstanceID ORDER BY StepNumber,StepID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(getToDoSQL);
            database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.Guid, flowInstanceID);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gvToDoList.DataSource = dataTable;
            gvToDoList.DataBind();
        }

        protected void gvToDoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell tableCell_OperatorInfo = e.Row.Cells[2];
                TableCell tableCell_FlowCompleted = e.Row.Cells[3];

                if (tableCell_FlowCompleted.Text.ToLower().Equals("true"))
                {
                    tableCell_FlowCompleted.Text = "已完成";
                    e.Row.BackColor = System.Drawing.Color.LightGreen;                    
                }
                else
                {
                    tableCell_FlowCompleted.Text = "未完成";
                    e.Row.BackColor = System.Drawing.Color.Pink;

                    try
                    {
                        string stepID = gvToDoList.DataKeys[e.Row.RowIndex]["StepID"].ToString();
                        string sql = "SELECT OperatorInfo FROM dbo.FlowStepOperator WHERE StepID = @StepID";
                        Database database = DatabaseFactory.CreateDatabase();
                        DbCommand dbCommand = database.GetSqlStringCommand(sql);
                        database.AddInParameter(dbCommand, "@StepID", DbType.String, stepID);
                        DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

                        string operatorInfo = string.Empty;
                        foreach (DataRow dataRow in dataTable.Rows)
                        {
                            operatorInfo += dataRow["OperatorInfo"].ToString() + ", ";
                        }
                        tableCell_OperatorInfo.Text = operatorInfo.TrimEnd(' ', ',');                        
                    }
                    catch
                    {
                    
                    }
                }                
            }
        }
    }
}